package com.example.demo5.info.controller;

import com.example.demo5.common.utils.MyFileUtil;
import com.example.demo5.info.pojo.UploadResult;
import com.example.demo5.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@RestController
public class FileUploadController {
    @Autowired
    private MyFileUtil myFileUtil;

    //上传文件
    @PostMapping("/upload")
    public AjaxResult uplodad(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String path=myFileUtil.uploadFile(file,originalFilename);
        int lastIndex = path.lastIndexOf("/");
        String filename = path.substring(lastIndex + 1);
        return AjaxResult.success("static/"+filename);
    }
    //下载文件
    @GetMapping("/download")
    public  void download(HttpServletResponse response,String filePath) throws IOException {
        myFileUtil.downloadFile(response,filePath);
    }
}
